#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       sqlite3tohtml.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2012-03-18 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import sqlite3DB
import os

def sqlite3tohtml(dbname="d_ea.db", num=10000, fo="d"):
	tmphtml = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>%s</title>
	</head>
	<body>
		<h1>%s</h1>
		<div id="content">%s</div>
	</body>
</html>"""

	db = sqlite3DB.DbAction(dbname)
	db.dbLoad()

	for name, description in db.query("SELECT `name`, `description` FROM `product` WHERE flag=1;"):
		if name is None or description is None:
			continue

		frname = name[0].lower()
		if not os.path.exists("%s/%s" % (fo, frname)):
			os.makedirs("%s/%s" % (fo, frname))

		try:
			filePath = "%s/%s/%s.html" % (fo, frname, name)
			open(filePath, "w").write(tmphtml % (name, name, description))
		except:
			if os.path.isfile(filePath):
				os.remove(filePath)

	db.dbLoad()


if __name__ == "__main__":
	sqlite3tohtml()
